<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Facilitators extends Model
{

    use SoftDelete;

    

    // 表名
//    protected $name = 'facilitators';
    protected $name = 'manystore_shop';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
//        'starttime_text',
//        'endtime_text',
//        'is_open_text',
        'status_text'
    ];
    

    
    public function getIsOpenList()
    {
        return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsOpenTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_open']) ? $data['is_open'] : '');
        $list = $this->getIsOpenList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function store()
    {
        return $this->belongsTo('Manystore', 'id', 'shop_id', [], 'LEFT')->setEagerlyType(0);
    }
    public function brand()
    {
        return $this->belongsTo('Brand', 'brand_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function pshop()
    {
        return $this->belongsTo('Facilitators', 'pid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function facilitator()
    {
//        return $this->belongsTo('Facilitator', 'id', 'shop_id', [], 'LEFT')->setEagerlyType(0);
        return $this->hasOne('Facilitator', 'shop_id', 'id', [], 'LEFT')->setEagerlyType(1);
    }
}
